package com.example.mymediaplayback;

import java.io.File;
import java.io.IOException;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MyMediaPlayback extends Activity {
	public File SDCARD = Environment.getExternalStorageDirectory();
	
	public Button play_raw,play_local_url,play_http_url;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_media_playback);
		setView();
	}
	
	private void setView(){
		play_raw = (Button)findViewById(R.id.play_raw);
		play_raw.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				playLocalRaw(R.raw.motorcycle);
			}
		});
		
		play_local_url = (Button)findViewById(R.id.play_local_url);
		play_local_url.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(isExternalStorageWritable() == true){
					playLocalUrl(SDCARD.toString()+"/motorcycle.wav");
				}
			}
		});
		
		play_http_url = (Button)findViewById(R.id.play_http_url);
		play_http_url.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				playHttpUrl("http://www.mcjh.kl.edu.tw/usr/wdh/techedu/wavsound/motorcycle.wav");
			}
		});
	}
	
	private void playLocalRaw(int id){
		MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(), id);
		mediaPlayer.start(); // no need to call prepare(); create() does that for you
	}
	
	private void playHttpUrl(String httpurl){
		MediaPlayer mediaPlayer = new MediaPlayer();
		mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
		try {
			mediaPlayer.setDataSource(httpurl);
			mediaPlayer.prepare(); // might take long! (for buffering, etc)
			mediaPlayer.start();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void playLocalUrl(String url) {
		Uri myUri = Uri.parse(url); // initialize Uri here
		MediaPlayer mediaPlayer = new MediaPlayer();
		mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
		try {
			mediaPlayer.setDataSource(getApplicationContext(), myUri);
			mediaPlayer.prepare();
			mediaPlayer.start();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/* Checks if external storage is available for read and write */
	public boolean isExternalStorageWritable() {
	    String state = Environment.getExternalStorageState();
	    if (Environment.MEDIA_MOUNTED.equals(state)) {
	        return true;
	    }
	    return false;
	}
}
